package com.app.couponsharelife.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.listener.OnClickListener
import com.app.couponsharelife.ui.main.home.good.GoodDetailActivity
import com.youth.banner.Banner
import com.youth.banner.loader.ImageLoader
import kotlinx.android.synthetic.main.choiceness_rvitm.view.*
import kotlinx.android.synthetic.main.choiceness_rvtopitm.view.*
import java.text.DecimalFormat

/**
 *Created by xct on 2018/12/25
 *Description:新首页通用商品列表
 */
class NewHomeUniversalAdapter(private val context: Context, private val list: List<TaobaoGoodBean>,
                              private val bannerList:List<String>, private val listener: OnClickListener):RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val TYPETOP=0
    private val TYPEITMP=1
    private var topHolder:MyTopHolder?=null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (TYPETOP==viewType){
            val view= LayoutInflater.from(parent.context).inflate(R.layout.choiceness_rvtopitm,parent,false)
            topHolder=MyTopHolder(view)
            topHolder!!
        }else{
            val view= LayoutInflater.from(parent.context).inflate(R.layout.choiceness_rvitm,parent,false)
            MyHolder(view)
        }
    }

    override fun getItemCount(): Int {
        return list.size+1
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyTopHolder){
            holder.bannerView!!
                    .setImageLoader(GlideImageLoader())
                    .setOffscreenPageLimit(3)
                    .setOnBannerListener{
                        listener.click(it,"")
                    }
                    .setImages(bannerList)
                    .start()
        }else if (holder is MyHolder) {
            if (list.isNotEmpty()){
                if (position%2==1){
                    holder.viewLeft!!.visibility=View.VISIBLE
                    holder.viewRight!!.visibility=View.GONE
                }else{
                    holder.viewRight!!.visibility=View.VISIBLE
                    holder.viewLeft!!.visibility=View.GONE
                }
                val df = DecimalFormat("#0.00")
                val taobaoGoodBean=list[position-1]
                holder.goodsTitle!!.text = taobaoGoodBean.title
                holder.goodsOldType!!.text="${taobaoGoodBean.goodsSource}价"
                Glide.with(context).asBitmap()
                        .apply(RequestOptions().error(R.drawable.ic_app_place_holder3))
                        .load(taobaoGoodBean.pict_url)
                        .into(holder.goodsImag!!)
                holder.goodsTitle!!.text=taobaoGoodBean.title
                holder.goodsOldPrice!!.text=taobaoGoodBean.zk_final_price
                if (taobaoGoodBean.has_coupon){
                    holder.goodsCoupon!!.text=String.format("¥%s", df.format(taobaoGoodBean.coupon_price.toDouble()))
                    holder.couponLL!!.visibility=View.VISIBLE
                }else{
                    holder.couponLL!!.visibility=View.INVISIBLE
                }
                holder.goodsType!!.setImageResource(taobaoGoodBean.userType)
                holder.goodsAward!!.text="￥${taobaoGoodBean.vip_commission}"
                holder.goodsPrice!!.text="￥${taobaoGoodBean.final_price}"
                holder.goodsSales!!.text=taobaoGoodBean.volume
                holder.shopType!!.setImageResource(taobaoGoodBean.userType)
                //holder.itemView.setOnClickListener { listener.click(position, "") }
                holder.itemView.setOnClickListener {
                    setIntent(taobaoGoodBean)
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position==0){
            TYPETOP
        }else{
            TYPEITMP
        }
    }
    private fun setIntent(taobaoGoodBean:TaobaoGoodBean){
        val intent = Intent(context, GoodDetailActivity::class.java)
        intent.putExtra("good", taobaoGoodBean)
        context.startActivity(intent)
    }
    inner class MyTopHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        var bannerView:Banner?=null
        init {
            bannerView=itemView.choiceness_rvtopitm_mBanner
        }
    }
    inner class MyHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        var goodsTitle:TextView?=null
        var viewLeft:View?=null
        var viewRight:View?=null
        var goodsPrice:TextView?=null
        var goodsImag:ImageView?=null
        var goodsOldPrice:TextView?=null
        var goodsCoupon:TextView?=null
        var couponLL:LinearLayout?=null
        var goodsAward:TextView?=null
        var goodsSales:TextView?=null
        var goodsType:ImageView?=null
        var goodsOldType:TextView?=null
        var shopType:ImageView?=null
        init {
            goodsTitle=itemView.choiceness_rvitm_title
            viewLeft=itemView.choiceness_rvitm_left
            viewRight=itemView.choiceness_rvitm_right
            goodsImag=itemView.choiceness_rvitm_img
            goodsOldPrice=itemView.choiceness_rvitm_oldprice
            goodsCoupon=itemView.choiceness_rvitm_quanprice
            couponLL=itemView.choiceness_rvitm_llq1
            goodsAward=itemView.choiceness_rvitm_jiangprice
            goodsSales=itemView.choiceness_rvitm_yuenumber
            goodsType=itemView.choiceness_rvitm_imgtype
            goodsPrice=itemView.choiceness_rvitm_price
            goodsOldType=itemView.choiceness_rvitm_oldpricetype
            shopType=itemView.choiceness_rvitm_ivGoodsType
        }
    }
    inner class GlideImageLoader: ImageLoader(){
        override fun displayImage(context: Context, path: Any, imageView: ImageView) {
            imageView.scaleType=ImageView.ScaleType.CENTER_CROP
            /*val pading=AppUtils.dip2px(context,12f)
            imageView.setPadding(pading,0,pading,0)*/
            val string=path as String
            val url=if (string.startsWith("//")){
                Contacts.TCP +string
            }else if(string.startsWith("http")){
                string
            }else{
                Contacts.APP_LIFE_IMGURL +string
            }
            Glide.with(context).asBitmap()
                    .apply(RequestOptions().transform(RoundedCorners( 5)).error(R.drawable.ic_app_place_holder2))
                    .load(url)
                    .into(imageView)
        }
    }
}